package com.jiuyin.dianjing.api.loader;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.jiuyin.dianjing.api.data.BasicResponse;
import com.jiuyin.dianjing.api.interfaces.ApiInterface;
import com.jiuyin.dianjing.api.retrofit.RetrofitServiceManager;
import com.jiuyin.dianjing.util.AesUtil;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Description:
 * File: ApiLoader.java
 * Author: Administrator
 * Create: 2018/07/30 09:34
 *
 * @author Administrator
 */
public class ApiLoader extends ObjectLoader {
    private ApiInterface apiJsonInterface;

    public ApiLoader() {
        apiJsonInterface = null;
    }

    /**
     * post Response JsonObject
     *
     * @param api     String
     * @param content String
     * @return Observable
     */
    public Observable<BasicResponse<JsonObject>> postStrObserveJson(String api, String content) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), content);
        return observe(getApiJsonInterface().postObserveBasicJson(api, requestBody));
    }

    /**
     * get 请求，无请求体
     *
     * @param api
     * @return
     */
    public Observable<BasicResponse<String>> getStrObserveJsonAes(String api) {
        return observe(getApiJsonInterface().getObserveBasicStr(api));
    }

    /**
     * put Response String
     *
     * @param api     String
     * @param content String
     * @param key     String Json请求体 key
     * @return Observable
     */
    public Observable<BasicResponse<JsonArray>> putStrObserveJsonAes(String api, String content, String key) {
        JSONObject jsonObject = new JSONObject();
        String encodeMsg = AesUtil.encrypt(content, "");
        try {
            jsonObject.put(key, encodeMsg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toString());
        return observe(getApiJsonInterface().putObserveJsonArray(api, requestBody));
    }

    /**
     * post Response String
     * 获取订单
     *
     * @param api     String
     * @param content String
     * @param key     String Json请求体 key
     * @return Observable
     */
    public Observable<BasicResponse<String>> postStrObserveJsonAesOrder(String api, String content, String key) {
        JSONObject jsonObject = new JSONObject();
        String encodeMsg = AesUtil.encrypt(content, "");
        try {
            jsonObject.put(key, encodeMsg);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toString());
        return observe(getApiJsonInterface().postObserveJson(api, requestBody));
    }

    /**
     * 没有请求体的post方式
     *
     * @param api String
     * @return Observable
     */
    public Observable<BasicResponse<String>> postStrObserveJsonAesOrder(String api) {
        return observe(getApiJsonInterface().postObserveJson(api));
    }

    private ApiInterface getApiJsonInterface() {
        if (apiJsonInterface == null) {
            apiJsonInterface = RetrofitServiceManager.getJsonInstance().create(ApiInterface.class);
        }
        return apiJsonInterface;
    }
}
